//
//  YKDataHeaderView.m
//  YunKang
//
//  Created by leoking870 on 2017/10/5.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKDataHeaderView.h"
#import <ActionSheetPicker-3.0/ActionSheetDatePicker.h>
#import "YearMonthPikerView.h"
#import <UIView+PresentView.h>
#import <ActionSheetDatePicker.h>
#import <SWActionSheet.h>
@interface YKDataHeaderView ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *startTextField;
@property (nonatomic, strong) UITextField *endTextField;
@property (nonatomic, strong)NSDateFormatter *formatter;
@property (nonatomic, strong)NSDate *startDate;
@property (nonatomic, strong)NSDate *endDate;
@end
@implementation YKDataHeaderView

- (instancetype)init {
    if (self = [self initWithType:YKDataHeaderViewTypeYearMonth]) {
        
    }
    return self;
}

- (instancetype)initWithType:(YKDataHeaderViewType)type
{
    self = [super init];
    if (self) {
        self.type = type;
        [self addSubview:self.startTextField];
        [self addSubview:self.endTextField];
        self.formatter = [[NSDateFormatter alloc]init];
        if (type == YKDataHeaderViewTypeYearMonth) {
            self.formatter.dateFormat = @"yyyy-MM";
        }
        else {
            self.formatter.dateFormat = @"yyyy-MM-dd";
        }
        [self.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:13 tailSpacing:13];
        [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(11);
            make.bottom.equalTo(self.mas_bottom).offset(-11);
        }];
        
        UIView *bottomLine1 = [[UIView alloc]init];
        bottomLine1.backgroundColor = [UIColor tf_colorWithHexString:@"#d3d4d4"];
        [self addSubview:bottomLine1];
        [bottomLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.startTextField.mas_leading);
            make.trailing.equalTo(self.startTextField.mas_trailing);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo(.5f);
        }];
        UIView *bottomLine2 = [[UIView alloc]init];
        bottomLine2.backgroundColor = [UIColor tf_colorWithHexString:@"#d3d4d4"];
        [self addSubview:bottomLine2];
        [bottomLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.endTextField.mas_leading);
            make.trailing.equalTo(self.endTextField.mas_trailing);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo(.5f);
        }];
        
        NSDate *date = [NSDate date];
        NSDate *monthLate = [date tf_dateByAddingMonth:1];
        if (type == YKDataHeaderViewTypeYearMonthDay) {
            date = [self.formatter dateFromString:[NSString stringWithFormat:@"%d-01-01",[NSDate date].tf_year]];
            monthLate = [NSDate date];
        }
        
        
        
        self.startDate = date;
        self.endDate = monthLate;
        self.startTextField.text = [self.formatter stringFromDate:date];
        self.endTextField.text = [self.formatter stringFromDate:monthLate];
    }
    return self;
}

- (NSString *)start {
    return self.startTextField.text;
}

- (NSString *)end {
    return self.endTextField.text;
}

- (UITextField *)startTextField {
    if (!_startTextField) {
        _startTextField = [[UITextField alloc] init];
        _startTextField.placeholder = @"起始时间";
        UILabel *left = [[UILabel alloc]init];
        left.text = @"起：";
        left.font = [UIFont systemFontOfSize:15];
        left.textColor = [UIColor tf_colorWithHexString:@"#444444"];
        [left sizeToFit];
        _startTextField.leftViewMode = UITextFieldViewModeAlways;
        _startTextField.leftView = left;
        _startTextField.delegate = self;
    }
    return _startTextField;
}
- (UITextField *)endTextField {
    if (!_endTextField) {
        _endTextField = [[UITextField alloc] init];
        _endTextField.placeholder = @"终止时间";
        UILabel *left = [[UILabel alloc]init];
        left.text = @"止：";
        left.font = [UIFont systemFontOfSize:15];
        left.textColor = [UIColor tf_colorWithHexString:@"#444444"];
        [left sizeToFit];
        _endTextField.leftViewMode = UITextFieldViewModeAlways;
        _endTextField.leftView = left;
        _endTextField.delegate = self;
    }
    return _endTextField;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (self.type == YKDataHeaderViewTypeYearMonth) {
        if (textField == _startTextField) {
            YearMonthPikerView *ymPikerView = [[[NSBundle mainBundle] loadNibNamed:@"YearMonthPikerView" owner:nil options:nil] objectAtIndex:0];
            ymPikerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 200);
            [UIView presentView:ymPikerView inContainerView:[UIApplication sharedApplication].keyWindow  direction:PresentDirectionUp];
            @weakify(self)
            @weakify(ymPikerView);
            ymPikerView.sureBlock = ^(NSString *year, NSString *month) {
                NSString *string = [NSString stringWithFormat:@"%@-%@", year,month];
                NSDate *startDate = [self_weak_.formatter dateFromString:string];
                if (!self.endDate || [self.endDate compare:startDate] == NSOrderedDescending) {
                    self.startDate = startDate;
                    self_weak_.startTextField.text = string;
                    if (self.dateChangeBlock && self.startTextField.text.length > 0 && self.endTextField.text.length > 0) {
                        self.dateChangeBlock(self.startTextField.text, self.endTextField.text);
                    }
                }
                else {
                    [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"终止时间需要比起始时间晚"];
                }
                
                [UIView dismissView:ymPikerView_weak_ direction:PresentDirectionUp];
            };
            ymPikerView.cancelBlock = ^{
                [UIView dismissView:ymPikerView_weak_ direction:PresentDirectionUp];
            };
            
        }
        else {
            YearMonthPikerView *ymPikerView = [[[NSBundle mainBundle] loadNibNamed:@"YearMonthPikerView" owner:nil options:nil] objectAtIndex:0];
            ymPikerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 200);
            [UIView presentView:ymPikerView inContainerView:[UIApplication sharedApplication].keyWindow  direction:PresentDirectionUp];
            @weakify(self)
            @weakify(ymPikerView);
            ymPikerView.sureBlock = ^(NSString *year, NSString *month) {
                NSString *string = [NSString stringWithFormat:@"%@-%@", year,month];
                NSDate *endDate = [self_weak_.formatter dateFromString:string];
                if (!self.startDate || [endDate compare:self.startDate] == NSOrderedDescending) {
                    self_weak_.endTextField.text = string;
                    self_weak_.endDate = endDate;
                    if (self.dateChangeBlock && self.startTextField.text.length > 0 && self.endTextField.text.length > 0) {
                        self.dateChangeBlock(self.startTextField.text, self.endTextField.text);
                    }
                }
                else {
                    [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"终止时间需要比起始时间晚"];
                }
                [UIView dismissView:ymPikerView_weak_ direction:PresentDirectionUp];
            };
            ymPikerView.cancelBlock = ^{
                [UIView dismissView:ymPikerView_weak_ direction:PresentDirectionUp];
            };
        }
    }
    else {
        if (self.startTextField == textField) {
            
            [ActionSheetDatePicker showPickerWithTitle:@"选择时间"
                                        datePickerMode:UIDatePickerModeDate
                                          selectedDate:self.startDate
                                             doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
                                                 NSDate *startDate = selectedDate;
                                                 if (!self.endDate || [self.endDate compare:startDate] == NSOrderedDescending) {
                                                     self.startDate = startDate;
                                                     self.startTextField.text = [self.formatter stringFromDate:self.startDate];
                                                     if (self.dateChangeBlock && self.startTextField.text.length > 0 && self.endTextField.text.length > 0) {
                                                         self.dateChangeBlock(self.startTextField.text, self.endTextField.text);
                                                     }
                                                 }
                                                 else {
                                                     [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"终止时间需要比起始时间晚"];
                                                 }
                                             }
                                           cancelBlock:nil
                                                origin:[UIApplication sharedApplication].keyWindow];
        }
        else {
            [ActionSheetDatePicker showPickerWithTitle:@"选择时间"
                                        datePickerMode:UIDatePickerModeDate
                                          selectedDate:self.endDate
                                             doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
                                                 NSDate *endDate = selectedDate;
                                                 NSString *string = [self.formatter stringFromDate:selectedDate];
                                                 if (!self.startDate || [endDate compare:self.startDate] == NSOrderedDescending) {
                                                     self.endTextField.text = string;
                                                     self.endDate = endDate;
                                                     if (self.dateChangeBlock && self.startTextField.text.length > 0 && self.endTextField.text.length > 0) {
                                                         self.dateChangeBlock(self.startTextField.text, self.endTextField.text);
                                                     }
                                                 }
                                                 else {
                                                     [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"终止时间需要比起始时间晚"];
                                                 }
                                             }
                                           cancelBlock:nil
                                                origin:[UIApplication sharedApplication].keyWindow];
        }
    }
    
    
    return NO;
}

@end
